package com.qianchang.microfinance.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.dm.library.http.base.HttpBase;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.dm.library.utils.SharedPreferenceUtils;
import com.dm.library.widgets.element.DTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/9 14:02
 */


public class GuideActivity extends BaseActivity {

    @BindView(R.id.viewpager_guide)
    ViewPager viewpagerGuide;
    @BindView(R.id.btn_next)
    DTextView btnNext;

//    private static int[] guideImgs = new int[]{R.drawable.guide1, R.drawable.guide2, R.drawable.guide3};
    private static int[] guideImgs = new int[]{R.drawable.pic_yindaoye_one, R.drawable.pic_yindaoye_two, R.drawable.pic_yindaoye_three};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initView();
    }


    protected void initView() {
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(this);
        viewpagerGuide.setAdapter(myPagerAdapter);
        viewpagerGuide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == guideImgs.length - 1) {
                    btnNext.setVisibility(View.VISIBLE);
                } else {
                    btnNext.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @OnClick({R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_next:
                if (initCheckLogin()){
                    gotoMain();
                }
                break;
        }
    }


    /**
     * 用户登陆检查
     */
    private boolean initCheckLogin(){
        HttpBase.LoginCheckListener listener = (HttpBase.LoginCheckListener) this;
        boolean isLogin = listener.checkLogin();
        if (!isLogin) {
            // 如果没有登录,跳转到登录页
            listener.goLogin();
            return false;
        }else{
            return true;
        }
    }

    private void gotoMain() {
        SharedPreferenceUtils.put(this, SharedPreferenceUtils.getKeyOfFirstRun(DMApplication.getInstance().getVersion()), false);
        GuideActivity.this.finish();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }


    private class MyPagerAdapter extends PagerAdapter {
        private Context context;

        // 界面列表


        public MyPagerAdapter(Context context) {
            this.context = context;
        }

        /**
         * 获得当前界面数
         */
        @Override
        public int getCount() {
            return guideImgs == null ? 0 : guideImgs.length;
        }

        /**
         * 初始化position位置的界面
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView image = new ImageView(context);
            image.setImageResource(guideImgs[position]);
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(image);
            return image;
        }

        /**
         * 判断是否由对象生成界面
         */
        @Override
        public boolean isViewFromObject(View view, Object arg1) {
            return (view == arg1);
        }

        /**
         * 销毁position位置的界面
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
